/**
	Copyright (c) 2009 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 1/14/2010
*/
@isTest
private class EmailToNewCaseTriggerTester {
	private static String cSubject = 'EmailToNewCase.Trigger test: ';
	private static String eSubject1 = cSubject + '[ ref : 00DR7C2j . 500R2BaMK : ref ]';
	private static String eSubject2 = cSubject + '[ ref:00D3ITK .50049D6fo:ref ]';
	private static String cDescription1 = 'Text Body : EmailToNewCase.Trigger test: michael shu test only '
	                                    + ' begining new line test \n';
	private static String cDescription2 = 'in the body '
	                                    + ' end new line test ';
	private static String eTextBody = cDescription1 + 'ref : 00DR7C2j . 500R2BaMK : ref' + cDescription2;
	private static User sysUser;

	private static void initialize(){
	    User u_new = new User();
		u_new.UserName = 'xyz_1234567890@atti-temp.com' + 'x';
		u_new.LastName = 'Michael Shu Test Email' + 'x';
		u_new.Email = 'xyz_1234567890@atti.com';
		u_new.Alias = 'abcde' + 'x';
		u_new.CommunityNickname = 'xyaz_1234556' + 'x';
		u_new.TimeZoneSidKey = 'America/Los_Angeles';
		u_new.LocaleSidKey = 'en_US';
		u_new.EmailEncodingKey = 'UTF-8';
		u_new.ProfileId = [select ID from Profile where Name = 'System Administrator' limit 1].Id;
		u_new.UserRoleId = [select ID from UserRole where Name = 'Information Systems' limit 1].Id;
		u_new.LanguageLocaleKey = 'en_US';
		Database.Saveresult sr = Database.insert(u_new);
		
		sysUser = [select ID from User where ID =: sr.getID() limit 1];
	}
	
	public static testMethod void testEmailSendToClosedCase() {
		initialize();
		List<Case> old_cases = new List<Case>();
		List<EmailMessage> old_messages = new List<EmailMessage>();
		
		Case cs = new Case();
		cs.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Case', 'IOG Incident');
		cs.CSS_CRF_RequestType__c = 'Inquiry';
		cs.CSS_ProductFamily__c = 'Product1;Product2';
		cs.Status = 'Open';
		cs.Origin = 'Phone';
		cs.Priority = 'High';
		cs.CRF_PACKAGE__c  = 'IOG Change Task<col>loda 1.0<col>Feature Code<col>209<col>109<col>O<col><col>Product Issue<col>Product/Directory Code<col>UDAC Code<col>UDAC Description <row>';
		cs.CRF_PACKAGE__c += 'IOG Change Task<col>mobile website<col>Feature Code<col>209<col>109<col>O<col><col>Product Issue<col>Product/Directory Code<col>UDAC Code<col>UDAC Description <row>';
		Database.SaveResult case_rst;
		System.runAs(sysUser){
			case_rst = Database.insert(cs);
		}
		
		List<Case> stubs = [select id from Case where ParentId =: case_rst.getId()];
		
		System.assertEquals(2, stubs.size());
		for(Case c : stubs){
			c.Status = 'Cancelled';
		}
		System.runAs(sysUser){
			update stubs;
		}
		
		for(Integer i=0 ; i < stubs.size(); i++){
			EmailMessage e = new EmailMessage();
			e.ParentId = stubs[i].Id;
			if(i < 1){	
				e.Subject  = eSubject1 + '0' + i;
				e.TextBody = eTextBody + '0' + i;
			}else{
				e.Subject  = eSubject2 + i;
				e.TextBody = eTextBody + i;
			}
			e.Incoming = true;
			old_messages.add(e);
			
			e = new EmailMessage();
			e.ParentId = stubs[i].Id;
			if(i < 1){	
				e.Subject  = cSubject + '0' + i; // email message without ref Id in the subject
				e.TextBody = eTextBody + '0' + i;
			}else{
				e.Subject  = cSubject + i; // email message without ref Id in the subject
				e.TextBody = eTextBody + i;
			}
			e.Incoming = true;
			old_messages.add(e);
		}
		Database.SaveResult[] message_rsts;
		System.runAs(sysUser){
			message_rsts = Database.insert(old_messages);
		} 	
		System.assertEquals(stubs.size() * 2, message_rsts.size());
		
		Set<ID> messageIDs = new Set<ID>();
		for(Integer i = 0; i < message_rsts.size(); i++){
			messageIDs.add(message_rsts[i].getId());
		}
		List<EmailMessage> messages	= new List<EmailMessage>([select Id, ParentId, Subject, TextBody, HtmlBody
		                                                      from EmailMessage 
		                                                      where Id IN: messageIDs Order By Subject]);
		System.assertEquals(message_rsts.size(), messages.size());
		
		Set<ID> newcasesIDs = new Set<ID>();
		for(Integer i = 0; i < messages.size(); i++){
			newcasesIDs.add(messages[i].ParentId);
		}		
		List<Case> newCases = new List<Case>([select Id, Subject, Description, Priority, Origin, Status,RecordTypeId, (select id from emailMessages) 
		                                      from Case
		                                      where Id IN: newcasesIDs Order By Subject]);
		System.assertEquals(2 , newCases.size());
		for(Case c : newCases){
			System.assertEquals(2 , c.emailMessages.size());
		}
		
		for(Integer i=0; i < newCases.size(); i ++ ){
			System.debug('-- Test0 --- :' + messages[i].Subject);
		}

		for(Integer i=0; i < newCases.size(); i ++ ){
			System.debug('-- Test1 --- :' + newCases[i].Subject);
		}
				
		for(Integer i=0; i < newCases.size(); i ++ ){
			System.assertEquals('Open', newCases[i].Status, 'Subject[' + i + '] = ' + messages[i].Subject);
			System.assertEquals('Email', newCases[i].Origin, 'Subject[' + i + '] = ' + messages[i].Subject);
			System.assertEquals('High', newCases[i].Priority, 'Subject[' + i + '] = ' + messages[i].Subject);
			System.assertEquals(null, messages[i].HtmlBody, 'Subject[' + i + '] = ' + messages[i].Subject);
		}
	
	}
	


}